package com.kehao.leetcode.jzoffer;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class JZSolution063 {
    public String replaceWords(List<String> dictionary, String sentence) {
        dictionary.sort(Comparator.comparingInt(String::length));
        String result = Arrays.stream(sentence.split(" ")).map(word -> {
            for (String dict : dictionary) {
                if (word.startsWith(dict)) {
                    return dict;
                }
            }
            return word;
        }).collect(Collectors.joining(" "));
        return result;
    }
}
